<!-- 出库离库单 -->
<template>
  <div class="container">
    <div ref="outAndInBIll" :style="`width: 100%; height: ${cardHeight}px`"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import throttle from '@/utils/throttle.js'

import { outAndInStatistics } from '@/api/rfid/board'

export default {
  name: 'outAndInBIll',
  components: {},
  props: {
    cardHeight: {
      type: Number,
      default: 250,
    },
  },
  data() {
    return {
      yearRange: new Date().getFullYear().toString(),
      outAndInStat: {},
      xAxisData: [
        '1月',
        '2月',
        '3月',
        '4月',
        '5月',
        '6月',
        '7月',
        '8月',
        '9月',
        '10月',
        '11月',
        '12月',
      ],
      myChart: null,
      throttledResize: null,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getOutAndInStatistics(this.yearRange)
    this.throttledResize = throttle(this.onWindowResize, 200)
    window.addEventListener('resize', this.throttledResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.throttledResize)
  },
  methods: {
    // 获取在库离库数据
    getOutAndInStatistics(val) {
      outAndInStatistics({ year: val }).then((res) => {
        if (res.data.code === 200) {
          this.outAndInStat = res.data.data
          this.initOutInListEcharts()
        }
      })
    },
    onWindowResize() {
      this.myChart.resize()
    },

    initOutInListEcharts() {
      var chartDom = this.$refs.outAndInBIll
      this.myChart = echarts.init(chartDom)
      var option
      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985',
            },
          },
        },
        legend: {
          data: ['出库', '入库'],
        },
        // toolbox: {
        //   feature: {
        //     saveAsImage: {},
        //   },
        // },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: this.xAxisData,
          },
        ],
        yAxis: [
          {
            type: 'value',
          },
        ],
        series: [
          {
            name: '出库',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
              focus: 'series',
            },
            data: this.outAndInStat.outData,
          },
          {
            name: '入库',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
              focus: 'series',
            },
            data: this.outAndInStat.inData,
          },
        ],
      }

      option && this.myChart.setOption(option)
    },
  },
}
</script>
<style lang="less" scoped></style>
